#include "../LogicSql_CommHeader.h"
#include "LogicSql_DevRoot.h"
CLogicSql_DevRoot::CLogicSql_DevRoot()
{
}
CLogicSql_DevRoot::~CLogicSql_DevRoot()
{
}
//////////////////////////////////////////////////////////////////////////
//                       公有函数
//////////////////////////////////////////////////////////////////////////
BOOL CLogicSql_DevRoot::LogicSql_DevRoot_Init(LPCTSTR lpszSQLAddr,int nPort,LPCTSTR lpszUser,LPCTSTR lpszPass)
{
    SQLPacket_IsErrorOccur = FALSE;

    if ((NULL == lpszSQLAddr) || (NULL == lpszUser) || (NULL == lpszPass))
    {
        SQLPacket_IsErrorOccur = TRUE;
        SQLPacket_dwErrorCode = ERROR_WINAPT_LOGIC_SQLPACKET_DEVROOT_INIT_PARAMENT;
        return FALSE;
    }
    DATABASE_MYSQL_CONNECTINFO st_DBConnectInfo;
    memset(&st_DBConnectInfo,'\0',sizeof(DATABASE_MYSQL_CONNECTINFO));

    st_DBConnectInfo.nPort = nPort;
    _tcscpy(st_DBConnectInfo.tszHostAddr,lpszSQLAddr);
    _tcscpy(st_DBConnectInfo.tszName,lpszUser);
    _tcscpy(st_DBConnectInfo.tszPassWord,lpszPass);
    _tcscpy(st_DBConnectInfo.tszDBName,_T("WINApt_SoftList"));

    if (!DataBase_MySQL_Connect(&xhData,&st_DBConnectInfo))
    {
        SQLPacket_IsErrorOccur = TRUE;
        SQLPacket_dwErrorCode = DataBase_GetLastError();
        return FALSE;
    }

    return TRUE;
}
BOOL CLogicSql_DevRoot::LogicSql_DevRoot_Close()
{
    DataBase_MySQL_Close(xhData);
    return TRUE;
}
BOOL CLogicSql_DevRoot::LogicSql_DevRoot_GetAll(LPCTSTR lpszName,list<LOGICSQL_SOFTLIST> *pStl_SoftList)
{
    SQLPacket_IsErrorOccur = TRUE;

    if ((NULL == pStl_SoftList) || (NULL == lpszName))
    {
        SQLPacket_IsErrorOccur = TRUE;
        SQLPacket_dwErrorCode = ERROR_WINAPT_LOGIC_SQLPACKET_DEVROOT_GETALL_PARAMENT;
        return FALSE;
    }
    DWORD64 dwLine = 0;
    DWORD64 dwField = 0;
    XNETHANDLE xhResult;
    TCHAR tszSQLQuery[2048];
    memset(tszSQLQuery,'\0',sizeof(tszSQLQuery));

    _stprintf_s(tszSQLQuery,_T("SELECT * FROM `PktDev_ListRoot` WHERE FullName LIKE '%%%s%%' AND Enable = '1'"),lpszName);
    if (!DataBase_MySQL_ExecuteQuery(xhData,&xhResult,tszSQLQuery,&dwLine,&dwField))
    {
        SQLPacket_IsErrorOccur = TRUE;
        SQLPacket_dwErrorCode = DataBase_GetLastError();
        return FALSE;
    }
    for (DWORD64 dwCount = 0;dwCount < dwLine;dwCount++)
    {
        TCHAR **pptszResult = DataBase_MySQL_GetResult(xhData,xhResult);
        LOGICSQL_SOFTLIST st_SoftList;
        memset(&st_SoftList,'\0',sizeof(LOGICSQL_SOFTLIST));

        if (NULL == pptszResult[0])
        {
            continue;
        }
        else
        {
            st_SoftList.nID = _ttoi(pptszResult[0]);
        }

        if (NULL != pptszResult[1])
        {
            _tcscpy(st_SoftList.tszName,pptszResult[1]);
        }
        if (NULL != pptszResult[2])
        {
            _tcscpy(st_SoftList.tszFullName,pptszResult[2]);
        }
        if (NULL != pptszResult[3])
        {
            _tcscpy(st_SoftList.tszVersion,pptszResult[3]);
        }
        if (NULL != pptszResult[4])
        {
            _tcscpy(st_SoftList.tszAuthor,pptszResult[4]);
        }
        if (NULL != pptszResult[5])
        {
            _tcscpy(st_SoftList.tszProvider,pptszResult[5]);
        }
        if (NULL != pptszResult[6])
        {
            _tcscpy(st_SoftList.tszBuildTime,pptszResult[6]);
        }
        if (NULL != pptszResult[7])
        {
            _tcscpy(st_SoftList.tszContact,pptszResult[7]);
        }
        if (NULL != pptszResult[8])
        {
            _tcscpy(st_SoftList.tszProjectUrl,pptszResult[8]);
        }
        if (NULL != pptszResult[9])
        {
            st_SoftList.nArchType = _ttoi(pptszResult[9]);
        }
        if (NULL != pptszResult[10])
        {
            st_SoftList.dlDlSize = _ttof(pptszResult[10]);
        }
        if (NULL != pptszResult[11])
        {
            st_SoftList.dlInstallSize = _ttof(pptszResult[11]);
        }
        if (NULL != pptszResult[12])
        {
            _tcscpy(st_SoftList.tszDlUrl,pptszResult[12]);
        }
        if (NULL != pptszResult[13])
        {
            _tcscpy(st_SoftList.tszFileMD5,pptszResult[13]);
        }
        if (NULL != pptszResult[14])
        {
            _tcscpy(st_SoftList.tszDepModule,pptszResult[14]);
        }
        if (NULL != pptszResult[15])
        {
            st_SoftList.bEnable = _ttoi(pptszResult[15]);
        }
        if (NULL != pptszResult[16])
        {
            _tcscpy(st_SoftList.tszDesInfo,pptszResult[16]);
        }
        if (NULL != pptszResult[17])
        {
            _tcscpy(st_SoftList.tszCreateTime,pptszResult[17]);
        }
        pStl_SoftList->push_back(st_SoftList);
    }
    DataBase_MySQL_FreeResult(xhData,xhResult);

    if (pStl_SoftList->empty())
    {
        SQLPacket_IsErrorOccur = TRUE;
        SQLPacket_dwErrorCode = ERROR_WINAPT_LOGIC_SQLPACKET_DEVROOT_GETALL_NODATA;
        return FALSE;
    }

    return TRUE;
}
BOOL CLogicSql_DevRoot::LogicSql_DevRoot_Insert(LOGICSQL_SOFTLIST *pSt_SoftList)
{
    SQLPacket_IsErrorOccur = TRUE;

    if (NULL == pSt_SoftList)
    {
        SQLPacket_IsErrorOccur = TRUE;
        SQLPacket_dwErrorCode = ERROR_WINAPT_LOGIC_SQLPACKET_DEVROOT_INSERT_PARAMENT;
        return FALSE;
    }
    TCHAR tszSQLQuery[2048];
    memset(tszSQLQuery,'\0',sizeof(tszSQLQuery));

    _stprintf_s(tszSQLQuery,"INSERT INTO `PktDev_ListRoot`(tszName,tszFullName,tszVersion,tszAuthor,tszProvider,tszBuildTime,tszContact,tszProjectUrl,nArchType,dlDlSize,dlInstallSize,tszDlUrl,tszFileMD5,tszDepModule,bEnable,tszDesInfo) VALUES('%s',%s,%s,%s,%s,%s,%s,%s,'%d','%lf','%lf','%s','%s','%s','%d','%s')",pSt_SoftList->tszName,pSt_SoftList->tszFullName,pSt_SoftList->tszVersion,pSt_SoftList->tszAuthor,pSt_SoftList->tszProvider,pSt_SoftList->tszBuildTime,pSt_SoftList->tszContact,pSt_SoftList->tszProjectUrl,pSt_SoftList->nArchType,pSt_SoftList->dlDlSize,pSt_SoftList->dlInstallSize,pSt_SoftList->tszDlUrl,pSt_SoftList->tszFileMD5,pSt_SoftList->tszDepModule,pSt_SoftList->bEnable,pSt_SoftList->tszDesInfo);

    if (!DataBase_MySQL_Execute(xhData,tszSQLQuery))
    {
        SQLPacket_IsErrorOccur = TRUE;
        SQLPacket_dwErrorCode = DataBase_GetLastError();
        return FALSE;
    }
    return TRUE;
}
